//
//  NSDate+Categories.m
//  BaseProject
//
//  Created by Guicai_Li on 3/13/14.
//  Copyright (c) 2014 Guicai_Li. All rights reserved.
//

#import "NSDate+Categories.h"
#import "NSUserDefaults+Categories.h"

@implementation NSDate (Categories)

/**
 *  获取系统时间(毫秒)
 *
 *  @return
 */
+ (NSString *)getSystemTimeString {
    return [NSString stringWithFormat:@"%lld", [self getSystemTimeInterval]];
}

+ (long long)getSystemTimeInterval {
    long long systemTime = [[NSDate date] timeIntervalSince1970] * 1000;
    return systemTime;
}

/**
 *  获取系统时间,按照指定格式
 *
 *  @param format 格式字符串
 *
 *  @return 指定格式时间字符串
 */
+ (NSString *)getSystemTimeStringWithFormat:(NSString *)format {
    NSString *date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    date = [formatter stringFromDate:[NSDate date]];
    return date;
}

/**
 *  时间戳转日期
 *
 *  @param timeInterval 时间(毫秒)
 *  @param formatString 格式字符串
 *
 *  @return 指定格式时间字符串
 */
+ (NSString *)secondToDate:(long long)timeInterval WithFormat:(NSString *)formatString {
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:formatString];
    return [dateFormat stringFromDate:date];
}

/**
 *  发言时间显示
 *
 *  @param timeInterval 时间戳
 *
 *  1.小于一分钟：刚刚
 *  2.小于一小时：x分钟前
 *  3.超过一小时，小于24小时：x小时前
 *  4.超过1天，小于7天：x天前
 *  5.其余显示：yyyy/MM/dd HH:mm
 *
 *  @return 指定格式字符串
 */
+ (NSString *)intervalSinceNow:(long long)timeInterval {
    NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval cha = (currentTime - timeInterval);
    NSString *timeString = @"";
    //NSNumber *longlongNumber=[NSNumber numberWithLongLong:timeInterval];
    //NSString *timeString=[longlongNumber stringValue];
    if (cha / 3600 < 1) {
        if (cha / 60 < 1) {
            timeString = @"刚刚";
        } else {
            timeString = [NSString stringWithFormat:@"%.f分钟前", cha / 60];
        }
    } else if (cha / 3600 > 1 && cha / 86400 < 1) {
        timeString = [NSString stringWithFormat:@"%.f小时前", cha / 3600];
    } else if (cha / 86400 > 1 && cha / 2592000 < 1) {
        timeString = [NSString stringWithFormat:@"%.f天前", cha / 86400];
    } else {
        timeString = [self secondToDate:timeInterval WithFormat:@"yyyy-MM-dd"];
    }
    return timeString;
}

/**
 *  根据时间戳算出年龄
 *
 */

+(NSInteger)ageWithTimeinterval:(long long)timeInterval{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitYear;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date toDate:nowDate options:0];
    
    NSInteger year = [comps year] + 1 ;
    return year;
}

/**
 *  算星座
 */
//+(NSString *)getAstroWithMonth:(int)m day:(int)d{
+(NSString *)getAstroWithTime:(NSString *)str{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str longLongValue]];
    NSCalendar *calendar = [NSCalendar currentCalendar];

   NSDateComponents *mcomps = [calendar components:NSCalendarUnitMonth fromDate:date];
    NSDateComponents *dcomps = [calendar components:NSCalendarUnitDay fromDate:date];
    NSInteger m =[mcomps month];
    NSInteger d = [dcomps day];
    
    NSString *astroString = @"摩羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手摩羯";
    
    NSString *astroFormat = @"102123444543";
    
    NSString *result;
    
    if (m<1||m>12||d<1||d>31){
        
        return @"错误日期格式!";
        
    }
    
    if(m==2 && d>29)
        
    {
        
        return @"错误日期格式!!";
        
    }else if(m==4 || m==6 || m==9 || m==11) {
        
        if (d>30) {
            
            return @"错误日期格式!!!";
            
        }
        
    }
    
    result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
    
    return result;
    
}



+ (BOOL)daysBetweenDate:(NSDate *)fromDateTime andDate:(NSDate *)toDateTime {
    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar rangeOfUnit:NSCalendarUnitDay startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSCalendarUnitDay startDate:&toDate
                 interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSCalendarUnitDay
                                               fromDate:fromDate toDate:toDate options:0];
    return [difference day] < 0;
}

+ (NSDate *)ChinaDate {
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    return [date dateByAddingTimeInterval:interval];
}

+ (void)saveLastOpenDate {
    [NSUserDefaults setObject:[self ChinaDate] forKey:@"LastOpenDate"];
    [NSUserDefaults synchronize];
}

+ (NSDate *)lastOpenDate {
    NSDate *date = (NSDate *) [NSUserDefaults objectForKey:@"LastOpenDate"];
    return (date == nil) ? [NSDate distantPast] : date;
}

+ (void)saveLastOpenDiagnoseDate {
    [NSUserDefaults setObject:[self ChinaDate] forKey:@"LastOpenDiagnoseDate"];
    [NSUserDefaults synchronize];
}

+ (NSDate *)lastOpenDiagnose {
    NSDate *date = (NSDate *) [NSUserDefaults objectForKey:@"LastOpenDiagnoseDate"];
    return (date == nil) ? [NSDate distantPast] : date;
}

@end
